
import java.awt.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TQNINH
 */
public class Order extends javax.swing.JFrame {

    private Connection cn = null;
    private final String url = "jdbc:sqlserver://";
    private final String serverName = "localhost";
    private final String portName = "1433";
    private final String instanceName = "SQL11";
    private final String databaseName = "DeveloperApps";
    private final String userName = "sa";
    private final String password = "";
    private PreparedStatement pstPizza;
    private ResultSet rsPizza = null;
    /** Creates new form Order */
    public Order() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            cn = DriverManager.getConnection(getConnectionUrl(), userName, password);
            if(cn != null) {
                JOptionPane.showMessageDialog(null, "Connection Successful!");
            }
            String strStmt = "SELECT ToppingPrice FROM ToppingDetails WHERE ToppingName = ?";
            pstPizza = cn.prepareStatement(strStmt);
        } catch(ClassNotFoundException | SQLException e) {
            JOptionPane.showMessageDialog(null, "Connection not Successful!");
        }
        initComponents();
    }
    
    private String getConnectionUrl() {
        //return url + serverName + ":" + portName + ";instanceName=" + instanceName + ";databaseName=" + databaseName;
        //return "jdbc:sqlserver://localhost\\SQL11:1433;databaseName=DeveloperApps";
        return "jdbc:sqlserver://localhost\\SQL11:1433;property=value";
    }
    
    private void closeConnection() {
        try {
            if(cn != null) {
                cn.close();
            } else {
                cn = null;
            }
        } catch(Exception e) {
            
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlType = new javax.swing.JPanel();
        radNonVeg = new javax.swing.JRadioButton();
        radVeg = new javax.swing.JRadioButton();
        pnlHeading = new javax.swing.JPanel();
        lblHeading = new javax.swing.JLabel();
        pnlFilling = new javax.swing.JPanel();
        chkChicken = new javax.swing.JCheckBox();
        chkPepperoni = new javax.swing.JCheckBox();
        chkCheese = new javax.swing.JCheckBox();
        chkOlives = new javax.swing.JCheckBox();
        pnlContactDetails = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblPhone = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        lblAddress = new javax.swing.JLabel();
        scpAddress = new javax.swing.JScrollPane();
        txaAddress = new javax.swing.JTextArea();
        btnSubmit = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        pnlResult = new javax.swing.JPanel();
        lblOrder = new javax.swing.JLabel();
        lblResult = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Order Form");
        setBounds(new java.awt.Rectangle(450, 50, 0, 0));
        setMinimumSize(new java.awt.Dimension(325, 595));
        setResizable(false);
        getContentPane().setLayout(null);

        pnlType.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pizza Type", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        radNonVeg.setText("Non-Vegetarian");
        pnlType.add(radNonVeg);

        radVeg.setText("Vegetarian");
        pnlType.add(radVeg);

        getContentPane().add(pnlType);
        pnlType.setBounds(10, 75, 300, 60);

        pnlHeading.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        pnlHeading.setLayout(null);

        lblHeading.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblHeading.setForeground(new java.awt.Color(183, 0, 0));
        lblHeading.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblHeading.setText("Pizza Italiano");
        pnlHeading.add(lblHeading);
        lblHeading.setBounds(0, 0, 300, 50);

        getContentPane().add(pnlHeading);
        pnlHeading.setBounds(10, 10, 300, 50);

        pnlFilling.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Choose Toppings", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        pnlFilling.setLayout(null);

        chkChicken.setText("Chicken");
        pnlFilling.add(chkChicken);
        chkChicken.setBounds(20, 20, 63, 23);

        chkPepperoni.setText("Pepperoni");
        pnlFilling.add(chkPepperoni);
        chkPepperoni.setBounds(20, 40, 73, 23);

        chkCheese.setText("Cheese");
        pnlFilling.add(chkCheese);
        chkCheese.setBounds(170, 40, 61, 23);

        chkOlives.setText("Olives");
        pnlFilling.add(chkOlives);
        chkOlives.setBounds(170, 20, 55, 23);

        getContentPane().add(pnlFilling);
        pnlFilling.setBounds(10, 150, 300, 70);

        pnlContactDetails.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Contact Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        pnlContactDetails.setLayout(null);

        lblName.setText("Name:");
        pnlContactDetails.add(lblName);
        lblName.setBounds(20, 30, 31, 14);
        pnlContactDetails.add(txtName);
        txtName.setBounds(80, 25, 200, 20);

        lblPhone.setText("Phone:");
        pnlContactDetails.add(lblPhone);
        lblPhone.setBounds(20, 60, 34, 14);

        txtPhone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPhoneKeyTyped(evt);
            }
        });
        pnlContactDetails.add(txtPhone);
        txtPhone.setBounds(80, 55, 200, 20);

        lblAddress.setText("Address:");
        pnlContactDetails.add(lblAddress);
        lblAddress.setBounds(20, 90, 43, 14);

        txaAddress.setColumns(20);
        txaAddress.setRows(5);
        scpAddress.setViewportView(txaAddress);

        pnlContactDetails.add(scpAddress);
        scpAddress.setBounds(80, 85, 200, 100);

        getContentPane().add(pnlContactDetails);
        pnlContactDetails.setBounds(10, 235, 300, 200);

        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });
        getContentPane().add(btnSubmit);
        btnSubmit.setBounds(70, 445, 75, 23);

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        getContentPane().add(btnExit);
        btnExit.setBounds(180, 445, 75, 23);

        pnlResult.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlResult.setLayout(null);

        lblOrder.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblOrder.setText("This Pizza will cost you: $10.0");
        lblOrder.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlResult.add(lblOrder);
        lblOrder.setBounds(10, 10, 280, 25);

        lblResult.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblResult.setText("Thanks for ordering from Pizza Italiano!!!");
        lblResult.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlResult.add(lblResult);
        lblResult.setBounds(10, 45, 280, 25);

        getContentPane().add(pnlResult);
        pnlResult.setBounds(10, 480, 300, 80);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtPhoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPhoneKeyTyped
        // TODO add your handling code here:
        String numbers = "0123456789 ";
        if(numbers.indexOf(evt.getKeyChar()) == -1) {
            evt.consume();
            lblResult.setText("Phone field accepts numberic value only!");
        }
    }//GEN-LAST:event_txtPhoneKeyTyped

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        // TODO add your handling code here:
        String[] parameters = new String[4];
        float fillingPrice = 0.0f;
        
        if(txaAddress.getText().length() == 0) {
            lblResult.setText("Address field cannot be left blank!");
        } else {
            Component[] boxes = pnlFilling.getComponents();
            int j = 0;
            
            for (int i = 0; i < boxes.length; i++) {
                JCheckBox box = (JCheckBox)boxes[i];
                if(box.isSelected()) {
                    JOptionPane.showMessageDialog(null, box.getText());
                    parameters[j] = box.getText();
                    j++;
                }
            }
            for (int i = 0; i < parameters.length; i++) {
                if(parameters[i] != null) {
                    try {
                        pstPizza.setString(1, parameters[i]);
                        rsPizza = pstPizza.executeQuery();

                        while(rsPizza.next()) {
                            fillingPrice = fillingPrice + rsPizza.getFloat("ToppingPrice");
                        }
                    } catch(SQLException e) {
                        
                    }
                }
            }
            lblOrder.setText("This Pizza will cost you: $ " + fillingPrice);
            lblResult.setText("Thanks for ordering from Pizza Italiano!");
        }
    }//GEN-LAST:event_btnSubmitActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        try {
            cn.commit();
            closeConnection();
        } catch(SQLException e) {
            
        }
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Order().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JCheckBox chkCheese;
    private javax.swing.JCheckBox chkChicken;
    private javax.swing.JCheckBox chkOlives;
    private javax.swing.JCheckBox chkPepperoni;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblHeading;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblOrder;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblResult;
    private javax.swing.JPanel pnlContactDetails;
    private javax.swing.JPanel pnlFilling;
    private javax.swing.JPanel pnlHeading;
    private javax.swing.JPanel pnlResult;
    private javax.swing.JPanel pnlType;
    private javax.swing.JRadioButton radNonVeg;
    private javax.swing.JRadioButton radVeg;
    private javax.swing.JScrollPane scpAddress;
    private javax.swing.JTextArea txaAddress;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables

}






























































